﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Copad.BusinessCard.DAL;
using Copad.BusinessCard.Model;

namespace Copad.BusinessCard.BLL
{
    public class GroupBLL : BLL<Group>
    {
        const string SYSTEM_USERNAME = "系统管理员";

        public bool IsExist(string name)
        {
            return dataBase.Count<Group>(x => x.Name == name) > 0;
        }

        public bool IsExist(long id, string name)
        {
            return dataBase.Count<Group>(x => x.Name == name && id != x.ID) > 0;
        }

        public List<GroupView> GetCardPermissions(long cardID)
        {
            List<GroupView> list = new List<GroupView>();
            var groupList = Get(x => x.Name != SYSTEM_USERNAME);
            groupList.ForEach(x =>
            {
                GroupView view = new GroupView { ID = x.ID, Name = x.Name, IsSelected = dataBase.Count<CardGroupPermissions>(w => w.CardID == cardID && w.GroupID == x.ID) > 0 };
                list.Add(view);
            });
            return list;
        }
    }
}
